package by.demchenko.sample.webapp.page.home.cards;

import by.demchenko.sample.datamodel.Course;
import by.demchenko.sample.datamodel.Person;
import by.demchenko.sample.datamodel.Schedule;
import by.demchenko.sample.datamodel.enums.ScheduleStatus;
import by.demchenko.sample.services.CourseService;
import by.demchenko.sample.services.UserService;
import by.demchenko.sample.webapp.app.BasicAuthenticationSession;
import by.demchenko.sample.webapp.page.home.BaseLayout;
import by.demchenko.sample.webapp.page.home.share.RegistrationPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.request.resource.PackageResourceReference;

import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.List;

public class TeacherCard extends BaseLayout {

    @Inject
    CourseService courseService;

    @Inject
    UserService userService;

    public TeacherCard(Person teacher) {
        //Roles roles = AuthenticatedWebSession.get().getRoles();

        add(new Image("teacherImage", new PackageResourceReference(BaseLayout.class, "/teachers/images/" + teacher.getId() + ".jpg")));
        add(new Label("teacher", teacher.getFullName(getSession().getLocale())));

        RepeatingView repeating = new RepeatingView("repeating");
        add(repeating);

        final List<Course> courses = courseService.getCourseRegistrationForTable(teacher);
        for (Course course : courses) {
            for (Schedule schedule : course.getSchedules()) {
                if (schedule.getStatus() != ScheduleStatus.OPENED) {
                    continue;
                }
                AbstractItem item = new AbstractItem(repeating.newChildId());
                repeating.add(item);
                item.add(new Label("course", course.getName()));
                item.add(new Label("section", course.getSection().getName(BasicAuthenticationSession.get().getLocale())));

                Link link = new Link("registrations") {
                    @Override
                    public void onClick() {
                        setResponsePage(new RegistrationPage(course, schedule.getStartDate()));
                    }
                };

                //if ((roles == null) || (!roles.hasRole("STUDENT"))) {
                //if (!BasicAuthenticationSession.get().isSignedIn()) {
                if (!BasicAuthenticationSession.get().userHasRole("STUDENT")) {
                    link.setEnabled(false);
                } else {
                    if (userService.isEnrolledInCourse(BasicAuthenticationSession.get().getUser(), course, schedule.getStartDate())) {
                        link.setEnabled(false);
                    }
                }

                item.add(link);
                SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
                link.add(new Label("registrations_text", DATE_FORMAT.format(schedule.getStartDate())));
            }
        }
    }
}
